/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JoInsertPanel.java
 *
 * Created on 29/11/2011, 22:31:05
 */

package proj5labbd.graphics.jornal;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import proj5labbd.DBInterface;
import proj5labbd.graphics.abstractOpGraphics.OperationPanel;

public class JoInsertPanel extends OperationPanel {

    /** Creates new form JoInsertPanel */
    public JoInsertPanel(JornalFrame frame, DBInterface dbInterface) {
        super(frame,dbInterface);
        this.setBounds(this.getX(), this.getY(), 300, this.getHeight());
        
        initComponents();
        jbtInsert.addActionListener(new JornalAction());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpnInsert = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbInsert = new javax.swing.JTable();
        jlbTitulo = new javax.swing.JLabel();
        jtxtTitulo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jlbDescricao = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtxaDesc = new javax.swing.JTextArea();
        jlbAbr = new javax.swing.JLabel();
        jcmbAbr = new javax.swing.JComboBox();
        jtxtDur = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jbtInsert = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Inserção"));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jpnInsert.setBorder(javax.swing.BorderFactory.createTitledBorder("Parâmetros de inserção"));

        jtbInsert.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbInsert);

        jlbTitulo.setText("Título");

        jLabel1.setText("Duração");

        jlbDescricao.setText("Descrição");

        jtxaDesc.setColumns(20);
        jtxaDesc.setRows(5);
        jScrollPane2.setViewportView(jtxaDesc);

        jlbAbr.setText("Abrangência");

        jcmbAbr.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "REGIONAL", "ESTADUAL", "NACIONAL", "INTERNACIONAL" }));

        jLabel2.setText("Departamento");

        javax.swing.GroupLayout jpnInsertLayout = new javax.swing.GroupLayout(jpnInsert);
        jpnInsert.setLayout(jpnInsertLayout);
        jpnInsertLayout.setHorizontalGroup(
            jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnInsertLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jlbDescricao, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpnInsertLayout.createSequentialGroup()
                            .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jtxtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jlbTitulo))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jpnInsertLayout.createSequentialGroup()
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(165, 165, 165))
                                .addComponent(jtxtDur)))
                        .addGroup(jpnInsertLayout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 159, Short.MAX_VALUE)
                            .addComponent(jlbAbr)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jcmbAbr, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE)))
                .addGap(15, 15, 15))
        );
        jpnInsertLayout.setVerticalGroup(
            jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnInsertLayout.createSequentialGroup()
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbTitulo)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtDur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jlbDescricao)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcmbAbr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlbAbr)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                .addContainerGap())
        );

        jbtInsert.setText("Inserir");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtInsert, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jpnInsert, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jpnInsert, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtInsert))
        );
    }// </editor-fold>//GEN-END:initComponents

    /*
     * Método para resgatar as informações das tabelas que serão exibidas
     */
    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        try {
            // TODO add your handling code here:
            setupTable();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Não foi possível resgatar as informações das tabelas");
            frame.printException(ex);
        }
    }//GEN-LAST:event_formComponentShown

    /*
     * Trata eventos do mouse
     */
    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
        jtbInsert.clearSelection();
    }//GEN-LAST:event_formMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtInsert;
    private javax.swing.JComboBox jcmbAbr;
    private javax.swing.JLabel jlbAbr;
    private javax.swing.JLabel jlbDescricao;
    private javax.swing.JLabel jlbTitulo;
    private javax.swing.JPanel jpnInsert;
    private javax.swing.JTable jtbInsert;
    private javax.swing.JTextArea jtxaDesc;
    private javax.swing.JTextField jtxtDur;
    private javax.swing.JTextField jtxtTitulo;
    // End of variables declaration//GEN-END:variables

    /*
     * Formata a tabela departamento para ser exibida
     */
    private void setupTable() throws SQLException {
            String[] data = {"idDe", "nomeDe"};
            String[] title = {"ID", "Nome do departamento"};
            String[] table = {"departamento"};
            DefaultTableModel model = new DefaultTableModel(dbInterface.view(data, null, table), title);
            jtbInsert.setModel(model);
            jtbInsert.setRowSelectionAllowed(true);
    }

    class JornalAction implements ActionListener {

       /*
        * Controla os eventos de inserção
        */
        public void actionPerformed(ActionEvent e) {
            if ( jtbInsert.getSelectedRowCount() == 1 && jtxtTitulo.getText().trim() != "") {
                    ArrayList<String> params = new ArrayList<String>();
                    ArrayList<String> fields = new ArrayList<String>();

                    params.add("SEQ_PROGRAMA_IDPR.NEXTVAL");
                    fields.add("idPr");
                    params.add("'"+jtxtTitulo.getText()+"'");
                    fields.add("tituloPr");

                    if (jtxaDesc.getText() != "") {
                        params.add("'"+jtxaDesc.getText()+"'");
                        fields.add("descricaoPr");
                    }
                    if (jtxtDur.getText() != "") {
                        params.add("'"+jtxtDur.getText()+"'");
                        fields.add("duracaoJo");
                    }

                    params.add("'"+jcmbAbr.getSelectedItem().toString()+"'");
                    fields.add("abrangenciaJo");

                    params.add((String)jtbInsert.getValueAt(jtbInsert.getSelectedRow(), 0));
                    fields.add("idDe");

                    String [] paramArray = new String[params.size()];
                    String [] fieldArray = new String[fields.size()];

                    params.toArray(paramArray);
                    fields.toArray(fieldArray);
                    try {
                        dbInterface.insert(fieldArray, paramArray, new String[]{"jornal"});
                    } catch (SQLException ex) {
                        frame.printException(ex);
                        JOptionPane.showMessageDialog(null, "Erro ao tentar inserir a tupla.");
                    }

                }
                else if (jtbInsert.getSelectedRowCount() != 1) {
                    JOptionPane.showMessageDialog(null, "Você deve escolher um departamento.");
                }
                else if (jtxtTitulo.getText().trim() == "") {
                    JOptionPane.showMessageDialog(null, "Você deve inserir o titulo do jornal.");
                }
        }

    }

}
